<?php
include_once('./config/init.php');

//判断是否有登录
AuthLogin();


//获取列表传递过来的id
$id = isset($_GET['id']) ? $_GET['id'] : 0;
$sql = "SELECT * FROM {$pre_}admin WHERE id = $id";
$admin = find($sql);
//ajax post数据
$action = isset($_POST['action']) ? $_POST['action'] : '';

//如果action == 'check' 我就知道他的请求是 ajax 异步请求过来的
if ($action == "check") {
    $success = [
        'result' => false, //成功 或者 失败
        'msg' => '', //文案信息
    ];

    $phone = isset($_POST['phone']) ? trim($_POST['phone']) : '';

    //判断是否为空
    if (empty($phone)) {
        $success['result'] = false;
        $success['msg'] = '手机号码不能为空';
    }

    //去查询数据库手机号是否存在
    $sql = "SELECT * FROM {$pre_}admin WHERE mobile = '$phone' AND id != '$id'";
    $result = find($sql);


    //如果不为空，就说明手机号存在，不能添加
    if ($result) {
        $success['result'] = false;
        $success['msg'] = '手机号已存在，不能注册';
    } else {
        //不存在，可以注册
        $success['result'] = true;
        $success['msg'] = '可以注册';
    }

    //将信息返回给前端
    //将php的数据转换为json
    echo json_encode($success);
    exit;
}

//判断是否有post表单
if ($_POST) {
    $nickname = isset($_POST['nickname']) ? trim($_POST['nickname']) : '';

    if (empty($nickname)) {
        ShowMsg('昵称不能为空');
        exit;
    }

    $phone = isset($_POST['phone']) ? trim($_POST['phone']) : '';

    if (empty($phone)) {
        ShowMsg('手机号不能为空');
        exit;
    }

    //判断手机号是否被注册
    $sql = "SELECT * FROM {$pre_}admin WHERE mobile = '$phone'";
    $check = find($sql);
    if ($check) {
        ShowMsg('手机号已存在');
        exit;
    }

    $password = isset($_POST['password']) ? trim($_POST['password']) : '';

    if (empty($password)) {
        ShowMsg('密码不能为空');
        exit;
    }

    $salt = "asd123";
    $repass = md5($password . $salt);
    $data = [
        'nickname' => trim($nickname),
        'mobile' => trim($phone),
        'lasttime' => time(),
        'salt' => trim($salt),
        'password' => trim($repass)
    ];

     //执行更新语句
     $affect = update('admin', $data, "id = $id");
    if ($affect) {
        ShowMsg('编辑管理员成功', 'admin_list.php');
        exit;
    } else {
        ShowMsg('编辑管理员失败');
        exit;
    }
}




?>

<!DOCTYPE html>
<html lang="en">

<head>
    <?php include_once('meta.php'); ?>
</head>

<body>
    <?php include_once('header.php'); ?>

    <?php include_once('menu.php'); ?>
    <div class="content">
        <div class="header">
            <h1 class="page-title">编辑管理员</h1>
        </div>

        <ul class="breadcrumb">
            <li>
                <a href="index.php">控制台</a>
                <span class="divider">/</span>
            </li>
            <li>
                <a href="admin_list.php">管理员列表</a>
                <span class="divider">/</span>
            </li>
            <li class="active">编辑管理员</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">
                <div class="btn-toolbar">
                    <button class="btn btn-primary" onClick="location='admin_list.php'">
                        <i class="icon-plus"></i>返回管理员列表
                    </button>
                </div>

                <!-- 表单 -->
                <div class="well">
                    <div id="myTabContent" class="tab-content">
                        <div class="tab-pane active in" id="home">
                            <form method="post" enctype="multipart/form-data">
                                <label>昵称</label>
                                <input type="text" name="nickname" class="input-xxlarge" placeholder="请输入昵称" required value="<?php echo $admin['nickname'];?>" />

                                <label>手机号</label>
                                <input type="text" name="phone" class="input-xxlarge" placeholder="请输入手机号" required value="<?php echo $admin['mobile']?>" />
                                <span id="notice" style="display:none;"></span>

                                <label>密码</label>
                                <input type="password" name="password" class="input-xxlarge" placeholder="请输入密码" required />

                                <label></label>
                                <input class="btn btn-primary" type="submit" value="提交" />
                            </form>
                        </div>
                    </div>
                </div>

                <footer>
                    <hr>
                    <p>&copy; 2017 <a href="#" target="_blank">copyright</a></p>
                </footer>
            </div>
        </div>
    </div>
</body>

</html>
<script>
    //表单提交事件
    $("person").submit(function() {
        //获取手机号、邮箱
        var phone = $("input[name=phone]").val()

        //手机号验证
        var PhoneReg = /^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d{8}$/;

        //判断手机号码正则
        if (!PhoneReg.test(phone)) {
            alert('手机号码填写格式有误')
            return false
        }
    })

    //当手机号改变的时候触发
    $("input[name=phone]").change(function() {
        var phone = $(this).val()

        //拿到内容，正则验证
        //手机号验证
        var PhoneReg = /^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d{8}$/;

        //判断手机号码正则
        if (!PhoneReg.test(phone)) {
            alert('手机号码填写格式有误')
            return false
        }

        //发送一个异步请求,到php服务器里面去
        $.ajax({
            //请求类型
            // async: true,  //默认为异步
            type: 'post',
            url: 'admin_edit.php',
            data: {
                action: 'check',
                phone: phone
            },
            dataType: 'json', //服务器返回类型
            success: function(success) {
                if (success.result) {
                    //可以注册
                    $("#notice").css({
                        display: 'inline',
                        color: 'green'
                    })

                    $("#notice").text(success.msg)
                } else {
                    //不能注册
                    $("#notice").css({
                        display: 'inline',
                        color: 'red'
                    })

                    $("#notice").text(success.msg)
                }
            }
        })
    })
</script>